package com.skiving.cloud.admin.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.skiving.cloud.common.utils.PageParameter;
import com.skiving.cloud.admin.entity.SysUserMenu;
import com.skiving.cloud.admin.mapper.SysUserMenuMapper;
import com.skiving.cloud.admin.service.SysUserMenuService;
import org.noear.solon.annotation.Component;
import org.noear.solon.Utils;

import java.util.List;

/**
 * 用户资源权限关系 服务层实现。
 *
 * @author skivingcloud
 * @since 2025-03-08
 */
@Component
public class SysUserMenuServiceImpl extends ServiceImpl<SysUserMenuMapper, SysUserMenu>  implements SysUserMenuService {
    /**
     * 分页查询用户资源权限关系
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<SysUserMenu> queryByPage(PageParameter<SysUserMenu> parameter){
        Page<SysUserMenu> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (Utils.isNotBlank(parameter.getParam().getId())) {
                queryWrapper.and(SysUserMenu::getId).like(parameter.getParam().getId());
            }
            if (Utils.isNotBlank(parameter.getParam().getUserId())) {
                queryWrapper.and(SysUserMenu::getUserId).like(parameter.getParam().getUserId());
            }
            if (Utils.isNotBlank(parameter.getParam().getMenuId())) {
                queryWrapper.and(SysUserMenu::getMenuId).like(parameter.getParam().getMenuId());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 根据用户id获取用户资源权限
     *
     * @param id 用户id
     * @return 资源权限
     */
    @Override
    public List<SysUserMenu> listByUserId(String id) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(SysUserMenu::getUserId).eq(id);
        return this.list(queryWrapper);
    }
}
